<template>
  <div class="page-list-live">
    <PageList>
      <template #header>部门管理</template>
      <template #filter>
        <ListFilter :form="topFilter" :key-list="keyList" @search="handleSearch" @reset="handleReset"></ListFilter>
      </template>
      <template #tips>
        <el-button icon="el-icon-delete" type="danger" @click="batchDeletion">批量删除</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="handleAdd">新增</el-button>
      </template>
      <template #table>
        <ListTable ref="RefListTable" :loading="tableLoading" :table-data="tableData" :table-col="tableCol" @selectChange="handleSelectionChange">
          <template slot="col1" slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              :loading="scope.row.loadingEdit"
              size="mini"
              style="margin-left: 0px; margin-right: 10px;"
              type="text"
              @click="handleRowEdit(scope.row)"
            >编辑</el-button>
            <el-button
              icon="el-icon-delete"
              style="color: red; margin-left: 0px; margin-right: 10px;"
              :loading="scope.row.loadingDel"
              size="mini"
              type="text"
              @click="handleRowDel(scope.row)"
            >删除</el-button>
          </template>
        </ListTable>
      </template>
    </PageList>
    <Department ref="RefAccountsEdit" @success="afterEditSuccess"></Department>
  </div>
</template>
<script>
  import listMixin from '@/mixins/list'
  import Department from './components/Department'
  import ListFilter from '@/components/ListFilter'
  import { department } from '@/api/api'
  export default {
    components: { Department, ListFilter },
    mixins: [listMixin],
    data() {
      return {
        deleteList: {
          id: ''
        },
        // 上级部门
        depList: [],
        // 查询结构
        topFilter: {
          keyword: { label: '关键字', placeholder: '请输入部门编码或部门名称、负责人' }
        },
        // 渲染的数据
        tableData: [],
        // 所有列表数据
        allListData: [],
        keyList: ['deptCode', 'deptName', 'responsibleMan', 'responsiblePhone'],
        // 列表表头
        tableCol: [
          { type: 'selection' },
          { type: 'index', label: '序号', width: '60px' },
          { prop: 'parentName', label: '上级部门' },
          { prop: 'deptCode', label: '部门编码' },
          {
            prop: 'deptName',
            label: '部门名称'
          },
          { prop: 'responsibleMan', label: '负责人' },
          { prop: 'responsiblePhone', label: '负责人电话' },
          { label: '操作', custom: 'col1' }
        ]
      }
    },
    methods: {
      // 批量删除
      batchDeletion() {
        if (!this.deleteList.id) {
          this.$message.warning('请选择要删除的数据！')
          return
        }
        this.$_delConfirm('选中部门', () => {
          this.delRowReq(this.deleteList)
        })
      },
      // 选中事件
      handleSelectionChange(event) {
        if (event.length > 0) {
          let ary = []
          event.forEach(item => {
            ary.push(item.id)
          })
          if (ary.length > 1) {
            this.deleteList.id = ary.join(',')
          } else {
            this.deleteList.id = ary[0]
          }
        } else {
          this.deleteList.id = ''
        }
      },
      // 请求列表数据
      async getList(query) {
        this.tableLoading = true

        try {
          let obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false }
          const {
            data: { items: resData, count, pageIndex }
          } = await department('post', obj, false)

          // 如果不是查询某一个部门
          if (!query) {
            this.allListData = resData
          }
          this.tableData = []
          this.tableData = resData.map(item => {
            item.loadingEdit = false
            item.loadingDel = false
            return item
          })
          this.$nextTick(() => {
            this.$refs['RefListTable'].update({ page: pageIndex, count: count })
            this.tableLoading = false
          })
        } catch (err) {
          this.tableLoading = false
          this.tableData = []
        }
      },
      async afterEditSuccess() {
        this.getList(this.serchObj)
      },
      handleAdd() {
        this.$refs['RefAccountsEdit'].getDepartmentList()
        let objData = {
          // 进入哪一个程序
          status: 'add',
          apiKey: {
            add: '/Department', // 添加
          }
        }
        this.$refs['RefAccountsEdit'].openOne(objData)
      },
      handleRowEdit(row) {
        this.$refs['RefAccountsEdit'].getDepartmentList()
        let objData = {
          // 进入哪一个程序
          status: 'edit',
          // 源数据
          row: row,
          // 源id
          id: row.id,
          // 查询参数
          searchData: {
            url: '/Department/' + row.id,
            method: 'GET',
            params: null
          }
        }
        this.$refs['RefAccountsEdit'].openOne(objData)
      },
      async delRowReq(row) {
        try {
          let obj = { id: row.id, value: null }
          let res = await department('delete', obj, true)
          row.loadingDel = false
          if (res.code === 200) {
            this.getList(this.serchObj)
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          }
        } catch (err) {
          row.loadingDel = false
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      },
      handleRowDel(row) {
        row.loadingDel = true
        this.$_delConfirm(
          '该部门',
          () => {
            this.delRowReq(row)
          },
          () => {
            row.loadingDel = false
          }
        )
      }
    }
  }
</script>

<style lang="scss" scoped></style>
